/**
 * Created by hjqi on 2016/3/30.
 */
module.controller('ModifyReplicasCtrl', ["$scope","$cookies", "$stateParams","$mdDialog","k8sService","UpdateInsertDB", "params",
    function ($scope,$cookies,$stateParams,$mdDialog,k8sService,UpdateInsertDB,params) {

        $scope.rcdata = {};
        $scope._lock = false;
        var version;
        if( params.data && params.data.rcnum ){
            $scope.rcdata.scaleNum = parseInt(params.data.rcnum);
        }else{
            $scope.rcdata.scaleNum = 0 ;
        }
        version = params.data.ver;
        $scope.ok = function(){
            //console.log("->>"+ $scope.rcdata.scaleNum ) ;
            //
            if( !isNum( $scope.rcdata.scaleNum ) ){
                return ;
            }

            var replicaController = params.data.rc ? params.data.rc : params.data.petset;
            if(params.data.id === undefined){
                //check appName exists?
                var _oldRcNum = params.data.rcnum;
                params.data.rcnum = $scope.rcdata.scaleNum;
                alertify.success(params.data.app+'scale from ['+_oldRcNum+'] to ['+$scope.rcdata.scaleNum+'] success');
                $mdDialog.hide(params.data);
            }else{
                if($scope._lock === false){
                    $scope._lock = true;
                    if(params.data.totalStatue === 'running'){
                        var _oldRcNum =  params.data.rcnum;
                        k8sService.modifyReplicas(params.data,$scope.rcdata.scaleNum).then(function(response){
                            if( response && response.ret != true  ){
                                alertify.alert('scale error',response.data );
                                $scope._lock = false;
                                $mdDialog.hide(params.data);
                                return ;
                            }

                            /*data.rcnum = $scope.rcdata.scaleNum;
                             replicaController.spec.replicas = $scope.rcdata.scaleNum;*/
                            params.data = response.data;
                            params.data.totalStatue = 'scaling';

                            /**
                             *   don't know why put update db in here , not in backend.
                             *   should review in future
                             */
                            /*if (!data.petset)
                             updateServiceInfoInDB(_oldRcNum,data);
                             else*/
                            $scope._lock = false;
                            $mdDialog.hide(params.data);


                        },function (response) {
                            //err flow
                            alertify.alert('scale error',response.error);
                            $scope._lock = false;
                            $mdDialog.hide(response.error);
                        });
                    }else{
                        /*data.rcnum = $scope.rcdata.scaleNum;
                         //data.rc.spec.replicas = $scope.rcdata.scaleNum;
                         updateServiceInfoInDB(_oldRcNum,data);*/
                        alertify.success(params.data.app+'scale from ['+_oldRcNum+'] to ['+params.data.rcnum+'] success');
                    }
                }


            }

        }
        $scope.cancel = function(){
            $mdDialog.cancel();
        };

        $scope.increase = function(num){
            if( ($scope.rcdata.scaleNum+'') === '0' && num===1 ){
                $scope.rcdata.scaleNum =1;
                return ;
            }
            if(  $scope.rcdata.scaleNum && isNum(  $scope.rcdata.scaleNum ) ){
                $scope.rcdata.scaleNum = parseInt( $scope.rcdata.scaleNum) + num ;
                if( $scope.rcdata.scaleNum === 0 ){
                    alertify.alert('scale error',"请输入大于零的整数!");
                    $scope.rcdata.scaleNum =1;
                }
            }
        };

        function updateServiceInfoInDB(oldRcNum,data){
            UpdateInsertDB.async(data).then(function(resp){
                //alertify.success(data.app+'scale from ['+oldRcNum+'] to ['+data.rcnum+'] success');
                $mdDialog.hide(data);
            },function(resp){
                //error flow
                alertify.alert('scale error',resp.data);
            });
        }

        function isNum(ss) {

            if ( !/^[0-9]*[1-9][0-9]*$/.test( ss ) ) {
                alertify.alert('scale error',"请输入大于零的整数!");
                return false ;
            }
            return true ;
        }
    }
]);